package forms;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class AddressSubmitForm extends ActionForm {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String country = null;

	
	private String street = null;
	private String city = null;
	private String state = null;
	private int zipCode = 0;
	private int areaCode = 0;
	private String phoneNumber = null;
	private int countryTelephoneCode = 0;
	private boolean us = false;
	
	
	
	
	public String getCountry() {
		return country;
	}




	public void setCountry(String country) {
		this.country = country;
	}




	public String getStreet() {
		return street;
	}




	public void setStreet(String street) {
		this.street = street;
	}




	public String getCity() {
		return city;
	}




	public void setCity(String city) {
		this.city = city;
	}




	public String getState() {
		return state;
	}




	public void setState(String state) {
		this.state = state;
	}




	public int getZipCode() {
		return zipCode;
	}




	public void setZipCode(int zipCode) {
		this.zipCode = zipCode;
	}




	public int getAreaCode() {
		return areaCode;
	}




	public void setAreaCode(int areaCode) {
		this.areaCode = areaCode;
	}




	public String getPhoneNumber() {
		return phoneNumber;
	}




	public void setPhoneNumber(String phoneNumber) {
		if(phoneNumber != null){
			String tmp = phoneNumber.replaceAll("\\-", "");
			phoneNumber = tmp.replaceAll("\\s", "");
		}
		this.phoneNumber = phoneNumber;
	}




	public int getCountryTelephoneCode() {
		return countryTelephoneCode;
	}




	public void setCountryTelephoneCode(int countryTelephoneCode) {
		this.countryTelephoneCode = countryTelephoneCode;
	}




	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
	public boolean isUs() {
		return us;
	}




	public void setUs(boolean us) {
		this.us = us;
	}




	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
		ActionErrors errors = new ActionErrors();
		
		
		
		Pattern regEx = Pattern.compile("\\D");
		
		
		if ((street == null) || (street.length() < 1))
			errors.add("streetMsgTagl", new ActionMessage("errors.required", "Street"));
		if(us){
			if ((state == null) || (state.length() < 1))
				errors.add("stateMsgTagl", new ActionMessage("errors.required", "State"));
		}
		
		if ((city == null) || (city.length() < 1))
			errors.add("cityMsgTagl", new ActionMessage("errors.required", "City"));
		
		Matcher matcher = regEx.matcher(((Integer)zipCode).toString());
		if ((zipCode == 0) || (zipCode < 10000|| zipCode > 99999))
			errors.add("zipCodeMsgTagl", new ActionMessage("errors.required", "Zip Code"));
		
		matcher.reset(((Integer)areaCode).toString());
		if ((areaCode == 0) || (areaCode < 100) || matcher.find())
			errors.add("areaCodeMsgTagl", new ActionMessage("errors.required", "Area Code"));
		
		
		//String tmp = Pattern.compile("\\s").matcher(((Integer)phoneNumber).toString()).replaceAll("");
		//phoneNumber = Integer.parseInt(tmp);
		
		//phoneNumber = Integer.parseInt(Pattern.compile("\\-").matcher(tmp).replaceAll(""));
		
		matcher.reset(phoneNumber);
		int tmp = Integer.parseInt(phoneNumber);
		
		if ((tmp == 0) || (tmp < 1000000)||(tmp > 9000000) || matcher.find())
			errors.add("phoneNumberMsgTagl", new ActionMessage("errors.required", "Phone Number"));
		
		if(!us){
			if ((countryTelephoneCode <= 0) || (countryTelephoneCode > 99))
				errors.add("countryTelephoneCodeMsgTagl", new ActionMessage("errors.required", "Country Telephone Code"));
		}
		return errors;
	}



	public void reset(ActionMapping mapping, HttpServletRequest request){
		setStreet(null);
		setState(null);
		setCity(null);
		setZipCode(0);
		setAreaCode(0);
		setCountryTelephoneCode(0);
		setPhoneNumber(null);
		
	}
}

